home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / infcmd.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  32KB  |  1,146 lines

  1. /* Memory-access and commands for inferior process, for GDB.
  2.    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include <signal.h>
  22. #include <sys/param.h>
  23. #include <string.h>
  24. #include "defs.h"
  25. #include "param.h"
  26. #include "symtab.h"
  27. #include "frame.h"
  28. #include "inferior.h"
  29. #include "environ.h"
  30. #include "value.h"
  31. #include "gdbcmd.h"
  32. #include "gdbcore.h"
  33. #include "target.h"
  34.  
  35. extern char *sys_siglist[];
  36.  
  37. extern void until_break_command ();    /* breakpoint.c */
  38.  
  39. #define ERROR_NO_INFERIOR \
  40.    if (!target_has_execution) error ("The program is not being run.");
  41.  
  42. /* String containing arguments to give to the program, separated by spaces.
  43.    Empty string (pointer to '\0') means no args.  */
  44.  
  45. static char *inferior_args;
  46.  
  47. /* File name for default use for standard in/out in the inferior.  */
  48.  
  49. char *inferior_io_terminal;
  50.  
  51. /* Pid of our debugged inferior, or 0 if no inferior now.
  52.    Since various parts of infrun.c test this to see whether there is a program
  53.    being debugged it should be nonzero (currently 3 is used) for remote
  54.    debugging.  */
  55.  
  56. int inferior_pid;
  57.  
  58. /* Last signal that the inferior received (why it stopped).  */
  59.  
  60. int stop_signal;
  61.  
  62. /* Address at which inferior stopped.  */
  63.  
  64. CORE_ADDR stop_pc;
  65.  
  66. /* Stack frame when program stopped.  */
  67.  
  68. FRAME_ADDR stop_frame_address;
  69.  
  70. /* Chain containing status of breakpoint(s) that we have stopped at.  */
  71.  
  72. bpstat stop_bpstat;
  73.  
  74. /* Flag indicating that a command has proceeded the inferior past the
  75.    current breakpoint.  */
  76.  
  77. int breakpoint_proceeded;
  78.  
  79. /* Nonzero if stopped due to a step command.  */
  80.  
  81. int stop_step;
  82.  
  83. /* Nonzero if stopped due to completion of a stack dummy routine.  */
  84.  
  85. int stop_stack_dummy;
  86.  
  87. /* Nonzero if stopped due to a random (unexpected) signal in inferior
  88.    process.  */
  89.  
  90. int stopped_by_random_signal;
  91.  
  92. /* Range to single step within.
  93.    If this is nonzero, respond to a single-step signal
  94.    by continuing to step if the pc is in this range.  */
  95.  
  96. CORE_ADDR step_range_start; /* Inclusive */
  97. CORE_ADDR step_range_end; /* Exclusive */
  98.  
  99. /* Stack frame address as of when stepping command was issued.
  100.    This is how we know when we step into a subroutine call,
  101.    and how to set the frame for the breakpoint used to step out.  */
  102.  
  103. FRAME_ADDR step_frame_address;
  104.  
  105. /* 1 means step over all subroutine calls.
  106.    -1 means step over calls to undebuggable functions.  */
  107.  
  108. int step_over_calls;
  109.  
  110. /* If stepping, nonzero means step count is > 1
  111.    so don't print frame next time inferior stops
  112.    if it stops due to stepping.  */
  113.  
  114. int step_multi;
  115.  
  116. /* Environment to use for running inferior,
  117.    in format described in environ.h.  */
  118.  
  119. struct environ *inferior_environ;
  120.  
  121. CORE_ADDR read_pc ();
  122. void breakpoint_clear_ignore_counts ();
  123.  
  124.  
  125. /* ARGSUSED */
  126. void
  127. tty_command (file, from_tty)
  128.      char *file;
  129.      int from_tty;
  130. {
  131.   if (file == 0)
  132.     error_no_arg ("terminal name for running target process");
  133.  
  134.   inferior_io_terminal = savestring (file, strlen (file));
  135. }
  136.  
  137. static void
  138. run_command (args, from_tty)
  139.      char *args;
  140.      int from_tty;
  141. {
  142.   char *exec_file;
  143.  
  144.   dont_repeat ();
  145.  
  146.   if (inferior_pid)
  147.     {
  148.       if (
  149.       !query ("The program being debugged has been started already.\n\
  150. Start it from the beginning? "))
  151.     error ("Program not restarted.");
  152.       target_kill ((char *)0, 0);
  153.     }
  154.  
  155.   exec_file = (char *) get_exec_file (0);
  156.  
  157.   /* The exec file is re-read every time we do an inferior_died, so
  158.      we just have to worry about the symbol file.  */
  159.   reread_symbols ();
  160.  
  161.   if (args)
  162.     {
  163.       char *cmd;
  164.       cmd = concat ("set args ", args, "");
  165.       make_cleanup (free, cmd);
  166.       execute_command (cmd, from_tty);
  167.     }
  168.  
  169.   if (from_tty)
  170.     {
  171.       printf ("Starting program: %s %s\n",
  172.           exec_file? exec_file: "", inferior_args);
  173.       fflush (stdout);
  174.     }
  175.  
  176.   target_create_inferior (exec_file, inferior_args,
  177.               environ_vector (inferior_environ));
  178. }
  179.  
  180. void
  181. continue_command (proc_count_exp, from_tty)
  182.      char *proc_count_exp;
  183.      int from_tty;
  184. {
  185.   ERROR_NO_INFERIOR;
  186.  
  187.   /* If have argument, set proceed count of breakpoint we stopped at.  */
  188.  
  189.   if (proc_count_exp != NULL)
  190.     {
  191.       bpstat bs = stop_bpstat;
  192.       int num = bpstat_num (&bs);
  193.       if (num == 0 && from_tty)
  194.     {
  195.       printf_filtered
  196.         ("Not stopped at any breakpoint; argument ignored.\n");
  197.     }
  198.       while (num != 0)
  199.     {
  200.       set_ignore_count (num,
  201.                 parse_and_eval_address (proc_count_exp) - 1,
  202.                 from_tty);
  203.       /* set_ignore_count prints a message ending with a period.
  204.          So print two spaces before "Continuing.".  */
  205.       if (from_tty)
  206.         printf ("  ");
  207.       num = bpstat_num (&bs);
  208.     }
  209.     }
  210.  
  211.   if (from_tty)
  212.     printf ("Continuing.\n");
  213.  
  214.   clear_proceed_status ();
  215.  
  216.   proceed ((CORE_ADDR) -1, -1, 0);
  217. }
  218.  
  219. /* Step until outside of current statement.  */
  220. static void step_1 ();
  221.  
  222. /* ARGSUSED */
  223. static void
  224. step_command (count_string, from_tty)
  225.      char * count_string;
  226.      int from_tty;
  227. {
  228.   step_1 (0, 0, count_string);
  229. }
  230.  
  231. /* Likewise, but skip over subroutine calls as if single instructions.  */
  232.  
  233. /* ARGSUSED */
  234. static void
  235. next_command (count_string, from_tty)
  236.      char * count_string;
  237.      int from_tty;
  238. {
  239.   step_1 (1, 0, count_string);
  240. }
  241.  
  242. /* Likewise, but step only one instruction.  */
  243.  
  244. /* ARGSUSED */
  245. static void
  246. stepi_command (count_string, from_tty)
  247.      char * count_string;
  248.      int from_tty;
  249. {
  250.   step_1 (0, 1, count_string);
  251. }
  252.  
  253. /* ARGSUSED */
  254. static void
  255. nexti_command (count_string, from_tty)
  256.      char * count_string;
  257.      int from_tty;
  258. {
  259.   step_1 (1, 1, count_string);
  260. }
  261.  
  262. static void
  263. step_1 (skip_subroutines, single_inst, count_string)
  264.      int skip_subroutines;
  265.      int single_inst;
  266.      char *count_string;
  267. {
  268.   register int count = 1;
  269.   FRAME fr;
  270.  
  271.   ERROR_NO_INFERIOR;
  272.   count = count_string ? parse_and_eval_address (count_string) : 1;
  273.  
  274.   for (; count > 0; count--)
  275.     {
  276.       clear_proceed_status ();
  277.  
  278.  
  279.       fr = get_current_frame ();
  280.       if (!fr)                /* Avoid coredump here.  Why tho? */
  281.     error ("No current frame");
  282.       step_frame_address = FRAME_FP (fr);
  283.  
  284.       if (! single_inst)
  285.     {
  286.       find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
  287.       if (step_range_end == 0)
  288.         {
  289.           int misc;
  290.  
  291.           misc = find_pc_misc_function (stop_pc);
  292.           target_terminal_ours ();
  293.           printf ("Current function has no line number information.\n");
  294.           fflush (stdout);
  295.  
  296.           /* No info or after _etext ("Can't happen") */
  297.           if (misc == -1 || misc == misc_function_count - 1)
  298.         error ("No data available on pc function.");
  299.  
  300.           printf ("Single stepping until function exit.\n");
  301.           fflush (stdout);
  302.  
  303.           step_range_start = misc_function_vector[misc].address;
  304.           step_range_end = misc_function_vector[misc + 1].address;
  305.         }
  306.     }
  307.       else
  308.     {
  309.       /* Say we are stepping, but stop after one insn whatever it does.
  310.          Don't step through subroutine calls even to undebuggable
  311.          functions.  */
  312.       step_range_start = step_range_end = 1;
  313.       if (!skip_subroutines)
  314.         step_over_calls = 0;
  315.     }
  316.  
  317.       if (skip_subroutines)
  318.     step_over_calls = 1;
  319.  
  320.       step_multi = (count > 1);
  321.       proceed ((CORE_ADDR) -1, -1, 1);
  322.       if (! stop_step)
  323.     break;
  324. #if defined (SHIFT_INST_REGS)
  325.       write_register (NNPC_REGNUM, read_register (NPC_REGNUM));
  326.       write_register (NPC_REGNUM, read_register (PC_REGNUM));
  327. #endif
  328.     }
  329. }
  330.  
  331. /* Continue program at specified address.  */
  332.  
  333. void
  334. jump_command (arg, from_tty)
  335.      char *arg;
  336.      int from_tty;
  337. {
  338.   register CORE_ADDR addr;
  339.   struct symtabs_and_lines sals;
  340.   struct symtab_and_line sal;
  341.  
  342.   ERROR_NO_INFERIOR;
  343.  
  344.   if (!arg)
  345.     error_no_arg ("starting address");
  346.  
  347.   sals = decode_line_spec_1 (arg, 1);
  348.   if (sals.nelts != 1)
  349.     {
  350.       error ("Unreasonable jump request");
  351.     }
  352.  
  353.   sal = sals.sals[0];
  354.   free (sals.sals);
  355.  
  356.   if (sal.symtab == 0 && sal.pc == 0)
  357.     error ("No source file has been specified.");
  358.  
  359.   if (sal.pc == 0)
  360.     sal.pc = find_line_pc (sal.symtab, sal.line);
  361.  
  362.   {
  363.     struct symbol *fn = get_frame_function (get_current_frame ());
  364.     struct symbol *sfn = find_pc_function (sal.pc);
  365.     if (fn != 0 && sfn != fn
  366.     && ! query ("Line %d is not in `%s'.  Jump anyway? ",
  367.             sal.line, SYMBOL_NAME (fn)))
  368.       error ("Not confirmed.");
  369.   }
  370.  
  371.   if (sal.pc == 0)
  372.     error ("No line %d in file \"%s\".", sal.line, sal.symtab->filename);
  373.  
  374.   addr = ADDR_BITS_SET (sal.pc);
  375.  
  376.   if (from_tty)
  377.     printf ("Continuing at %s.\n", local_hex_string(addr));
  378.  
  379.   clear_proceed_status ();
  380.   proceed (addr, 0, 0);
  381. }
  382.  
  383. /* Continue program giving it specified signal.  */
  384.  
  385. static void
  386. signal_command (signum_exp, from_tty)
  387.      char *signum_exp;
  388.      int from_tty;
  389. {
  390.   register int signum;
  391.  
  392.   dont_repeat ();        /* Too dangerous.  */
  393.   ERROR_NO_INFERIOR;
  394.  
  395.   if (!signum_exp)
  396.     error_no_arg ("signal number");
  397.  
  398.   signum = parse_and_eval_address (signum_exp);
  399.  
  400.   if (from_tty)
  401.     printf ("Continuing with signal %d.\n", signum);
  402.  
  403.   clear_proceed_status ();
  404.   proceed (stop_pc, signum, 0);
  405. }
  406.  
  407. /* Execute a "stack dummy", a piece of code stored in the stack
  408.    by the debugger to be executed in the inferior.
  409.  
  410.    To call: first, do PUSH_DUMMY_FRAME.
  411.    Then push the contents of the dummy.  It should end with a breakpoint insn.
  412.    Then call here, passing address at which to start the dummy.
  413.  
  414.    The contents of all registers are saved before the dummy frame is popped
  415.    and copied into the buffer BUFFER.
  416.  
  417.    The dummy's frame is automatically popped whenever that break is hit.
  418.    If that is the first time the program stops, run_stack_dummy
  419.    returns to its caller with that frame already gone.
  420.    Otherwise, the caller never gets returned to.  */
  421.  
  422. /* DEBUG HOOK:  4 => return instead of letting the stack dummy run.  */
  423.  
  424. static int stack_dummy_testing = 0;
  425.  
  426. void
  427. run_stack_dummy (addr, buffer)
  428.      CORE_ADDR addr;
  429.      char buffer[REGISTER_BYTES];
  430. {
  431.   /* Now proceed, having reached the desired place.  */
  432.   clear_proceed_status ();
  433.   if (stack_dummy_testing & 4)
  434.     {
  435.       POP_FRAME;
  436.       return;
  437.     }
  438.   proceed_to_finish = 1;    /* We want stop_registers, please... */
  439.   proceed (addr, 0, 0);
  440.  
  441.   if (!stop_stack_dummy)
  442.     /* This used to say
  443.        "Cannot continue previously requested operation".  */
  444.     error ("\
  445. The program being debugged stopped while in a function called from GDB.\n\
  446. The expression which contained the function call has been discarded.");
  447.  
  448.   /* On return, the stack dummy has been popped already.  */
  449.  
  450.   bcopy (stop_registers, buffer, sizeof stop_registers);
  451. }
  452.  
  453. /* Proceed until we reach a different source line with pc greater than
  454.    our current one or exit the function.  We skip calls in both cases.
  455.  
  456.    Note that eventually this command should probably be changed so
  457.    that only source lines are printed out when we hit the breakpoint
  458.    we set.  I'm going to postpone this until after a hopeful rewrite
  459.    of wait_for_inferior and the proceed status code. -- randy */
  460.  
  461. /* ARGSUSED */
  462. void
  463. until_next_command (from_tty)
  464.      int from_tty;
  465. {
  466.   FRAME frame;
  467.   CORE_ADDR pc;
  468.   struct symbol *func;
  469.   struct symtab_and_line sal;
  470.  
  471.   clear_proceed_status ();
  472.  
  473.   frame = get_current_frame ();
  474.  
  475.   /* Step until either exited from this function or greater
  476.      than the current line (if in symbolic section) or pc (if
  477.      not). */
  478.  
  479.   pc = read_pc ();
  480.   func = find_pc_function (pc);
  481.   
  482.   if (!func)
  483.     {
  484.       int misc_func = find_pc_misc_function (pc);
  485.       
  486.       if (misc_func != -1)
  487.     error ("Execution is not within a known function.");
  488.       
  489.       step_range_start = misc_function_vector[misc_func].address;
  490.       step_range_end = pc;
  491.     }
  492.   else
  493.     {
  494.       sal = find_pc_line (pc, 0);
  495.       
  496.       step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
  497.       step_range_end = sal.end;
  498.     }
  499.   
  500.   step_over_calls = 1;
  501.   step_frame_address = FRAME_FP (frame);
  502.   
  503.   step_multi = 0;        /* Only one call to proceed */
  504.   
  505.   proceed ((CORE_ADDR) -1, -1, 1);
  506. }
  507.  
  508. void 
  509. until_command (arg, from_tty)
  510.      char *arg;
  511.      int from_tty;
  512. {
  513.   if (!target_has_execution)
  514.     error ("The program is not running.");
  515.   if (arg)
  516.     until_break_command (arg, from_tty);
  517.   else
  518.     until_next_command (from_tty);
  519. }
  520.  
  521. /* "finish": Set a temporary breakpoint at the place
  522.    the selected frame will return to, then continue.  */
  523.  
  524. static void
  525. finish_command (arg, from_tty)
  526.      char *arg;
  527.      int from_tty;
  528. {
  529.   struct symtab_and_line sal;
  530.   register FRAME frame;
  531.   struct frame_info *fi;
  532.   register struct symbol *function;
  533.  
  534.   if (arg)
  535.     error ("The \"finish\" command does not take any arguments.");
  536.   if (!target_has_execution)
  537.     error ("The program is not running.");
  538.   if (selected_frame == NULL)
  539.     error ("No selected frame.");
  540.  
  541.   frame = get_prev_frame (selected_frame);
  542.   if (frame == 0)
  543.     error ("\"finish\" not meaningful in the outermost frame.");
  544.  
  545.   clear_proceed_status ();
  546.  
  547.   fi = get_frame_info (frame);
  548.   sal = find_pc_line (fi->pc, 0);
  549.   sal.pc = fi->pc;
  550.   set_momentary_breakpoint (sal, frame);
  551.  
  552.   /* Find the function we will return from.  */
  553.  
  554.   fi = get_frame_info (selected_frame);
  555.   function = find_pc_function (fi->pc);
  556.  
  557.   /* Print info on the selected frame, including level number
  558.      but not source.  */
  559.   if (from_tty)
  560.     {
  561.       printf_filtered ("Run till exit from ");
  562.       print_stack_frame (selected_frame, selected_frame_level, 0);
  563.     }
  564.  
  565.   proceed_to_finish = 1;        /* We want stop_registers, please... */
  566.   proceed ((CORE_ADDR) -1, -1, 0);
  567.  
  568.   if (bpstat_momentary_breakpoint (stop_bpstat) && function != 0)
  569.     {
  570.       struct type *value_type;
  571.       register value val;
  572.       CORE_ADDR funcaddr;
  573.  
  574.       value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
  575.       if (!value_type)
  576.     fatal ("internal: finish_command: function has no target type");
  577.       
  578.       if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
  579.     return;
  580.  
  581.       funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
  582.  
  583.       val = value_being_returned (value_type, stop_registers,
  584.           using_struct_return (value_of_variable (function),
  585.                    funcaddr,
  586.                    value_type,
  587.         BLOCK_GCC_COMPILED (SYMBOL_BLOCK_VALUE (function))));
  588.  
  589.       printf_filtered ("Value returned is $%d = ", record_latest_value (val));
  590.       value_print (val, stdout, 0, Val_no_prettyprint);
  591.       printf_filtered ("\n");
  592.     }
  593. }
  594.  
  595. /* ARGSUSED */
  596. static void
  597. program_info (args, from_tty)
  598.     char *args;
  599.     int from_tty;
  600. {
  601.   bpstat bs = stop_bpstat;
  602.   int num = bpstat_num (&bs);
  603.   
  604.   if (!target_has_execution)
  605.     {
  606.       printf ("The program being debugged is not being run.\n");
  607.       return;
  608.     }
  609.  
  610.   target_files_info ();
  611.   printf ("Program stopped at %s.\n", local_hex_string(stop_pc));
  612.   if (stop_step)
  613.     printf ("It stopped after being stepped.\n");
  614.   else if (num != 0)
  615.     {
  616.       /* There may be several breakpoints in the same place, so this
  617.      isn't as strange as it seems.  */
  618.       while (num != 0)
  619.     {
  620.       if (num < 0)
  621.         printf ("It stopped at a breakpoint that has since been deleted.\n");
  622.       else
  623.         printf ("It stopped at breakpoint %d.\n", num);
  624.       num = bpstat_num (&bs);
  625.     }
  626.     }
  627.   else if (stop_signal) {
  628. #ifdef PRINT_RANDOM_SIGNAL
  629.     PRINT_RANDOM_SIGNAL (stop_signal);
  630. #else
  631.     printf ("It stopped with signal %d (%s).\n",
  632.         stop_signal, 
  633.         (stop_signal > NSIG)? "unknown": sys_siglist[stop_signal]);
  634. #endif
  635.   }
  636.  
  637.   if (!from_tty)
  638.     printf ("Type \"info stack\" or \"info registers\" for more information.\n");
  639. }
  640.  
  641. static void
  642. environment_info (var)
  643.      char *var;
  644. {
  645.   if (var)
  646.     {
  647.       register char *val = get_in_environ (inferior_environ, var);
  648.       if (val)
  649.     printf ("%s = %s\n", var, val);
  650.       else
  651.     printf ("Environment variable \"%s\" not defined.\n", var);
  652.     }
  653.   else
  654.     {
  655.       register char **vector = environ_vector (inferior_environ);
  656.       while (*vector)
  657.     printf ("%s\n", *vector++);
  658.     }
  659. }
  660.  
  661. static void
  662. set_environment_command (arg)
  663.      char *arg;
  664. {
  665.   register char *p, *val, *var;
  666.   int nullset = 0;
  667.  
  668.   if (arg == 0)
  669.     error_no_arg ("environment variable and value");
  670.  
  671.   /* Find seperation between variable name and value */
  672.   p = (char *) strchr (arg, '=');
  673.   val = (char *) strchr (arg, ' ');
  674.  
  675.   if (p != 0 && val != 0)
  676.     {
  677.       /* We have both a space and an equals.  If the space is before the
  678.      equals and the only thing between the two is more space, use
  679.      the equals */
  680.       if (p > val)
  681.     while (*val == ' ')
  682.       val++;
  683.  
  684.       /* Take the smaller of the two.  If there was space before the
  685.      "=", they will be the same right now. */
  686.       p = arg + min (p - arg, val - arg);
  687.     }
  688.   else if (val != 0 && p == 0)
  689.     p = val;
  690.  
  691.   if (p == arg)
  692.     error_no_arg ("environment variable to set");
  693.  
  694.   if (p == 0 || p[1] == 0)
  695.     {
  696.       nullset = 1;
  697.       if (p == 0)
  698.     p = arg + strlen (arg);    /* So that savestring below will work */
  699.     }
  700.   else
  701.     {
  702.       /* Not setting variable value to null */
  703.       val = p + 1;
  704.       while (*val == ' ' || *val == '\t')
  705.     val++;
  706.     }
  707.  
  708.   while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
  709.  
  710.   var = savestring (arg, p - arg);
  711.   if (nullset)
  712.     {
  713.       printf ("Setting environment variable \"%s\" to null value.\n", var);
  714.       set_in_environ (inferior_environ, var, "");
  715.     }
  716.   else
  717.     set_in_environ (inferior_environ, var, val);
  718.   free (var);
  719. }
  720.  
  721. static void
  722. unset_environment_command (var, from_tty)
  723.      char *var;
  724.      int from_tty;
  725. {
  726.   if (var == 0)
  727.     {
  728.       /* If there is no argument, delete all environment variables.
  729.      Ask for confirmation if reading from the terminal.  */
  730.       if (!from_tty || query ("Delete all environment variables? "))
  731.     {
  732.       free_environ (inferior_environ);
  733.       inferior_environ = make_environ ();
  734.     }
  735.     }
  736.   else
  737.     unset_in_environ (inferior_environ, var);
  738. }
  739.  
  740. /* Handle the execution path (PATH variable) */
  741.  
  742. const static char path_var_name[] = "PATH";
  743.  
  744. /* ARGSUSED */
  745. void
  746. path_info (args, from_tty)
  747.      char *args;
  748.      int from_tty;
  749. {
  750.   printf ("Executable and object file path: %s\n", 
  751.       get_in_environ (inferior_environ, path_var_name));
  752. }
  753.  
  754. /* Add zero or more directories to the front of the execution path.  */
  755.  
  756. void
  757. path_command (dirname, from_tty)
  758.      char *dirname;
  759.      int from_tty;
  760. {
  761.   char *exec_path;
  762.  
  763.   dont_repeat ();
  764.   exec_path = strsave (get_in_environ (inferior_environ, path_var_name));
  765.   mod_path (dirname, &exec_path);
  766.   set_in_environ (inferior_environ, path_var_name, exec_path);
  767.   free (exec_path);
  768.   if (from_tty)
  769.     path_info ((char *)NULL, from_tty);
  770. }
  771.  
  772. CORE_ADDR
  773. read_pc ()
  774. {
  775.   return ADDR_BITS_REMOVE ((CORE_ADDR) read_register (PC_REGNUM));
  776. }
  777.  
  778. void
  779. write_pc (val)
  780.      CORE_ADDR val;
  781. {
  782.   write_register (PC_REGNUM, (long) val);
  783. #ifdef NPC_REGNUM
  784.   write_register (NPC_REGNUM, (long) val+4);
  785. #endif
  786.   pc_changed = 0;
  787. }
  788.  
  789. char *reg_names[] = REGISTER_NAMES;
  790.  
  791. /* Print out the machine register regnum. If regnum is -1,
  792.    print all registers (fpregs == 1) or all non-float registers
  793.    (fpregs == 0).
  794.  
  795.    For most machines, having all_registers_info() print the
  796.    register(s) one per line is good enough. If a different format
  797.    is required, (eg, for MIPS or Pyramid 90x, which both have
  798.    lots of regs), or there is an existing convention for showing
  799.    all the registers, define the macro DO_REGISTERS_INFO(regnum, fp)
  800.    to provide that format.  */  
  801.  
  802. #if !defined (DO_REGISTERS_INFO)
  803. #define DO_REGISTERS_INFO(regnum, fp) do_registers_info(regnum, fp)
  804. static void
  805. do_registers_info (regnum, fpregs)
  806.      int regnum;
  807.      int fpregs;
  808. {
  809.   register int i;
  810.  
  811.   for (i = 0; i < NUM_REGS; i++)
  812.     {
  813.       char raw_buffer[MAX_REGISTER_RAW_SIZE];
  814.       char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
  815.  
  816.       /* Decide between printing all regs, nonfloat regs, or specific reg.  */
  817.       if (regnum == -1) {
  818.     if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT && !fpregs)
  819.       continue;
  820.       } else {
  821.         if (i != regnum)
  822.       continue;
  823.       }
  824.  
  825.       fputs_filtered (reg_names[i], stdout);
  826.       print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
  827.  
  828.       /* Get the data in raw format, then convert also to virtual format.  */
  829.       if (read_relative_register_raw_bytes (i, raw_buffer))
  830.     {
  831.       printf_filtered ("Invalid register contents\n");
  832.       continue;
  833.     }
  834.       
  835.       target_convert_to_virtual (i, raw_buffer, virtual_buffer);
  836.  
  837.       /* If virtual format is floating, print it that way, and in raw hex.  */
  838.       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
  839.       && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
  840.     {
  841.       register int j;
  842.  
  843.       val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0,
  844.              stdout, 0, 1, 0, Val_pretty_default);
  845.  
  846.       printf_filtered ("\t(raw 0x");
  847.       for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
  848.         printf_filtered ("%02x", (unsigned char)raw_buffer[j]);
  849.       printf_filtered (")");
  850.     }
  851.  
  852. /* FIXME!  val_print probably can handle all of these cases now...  */
  853.  
  854.       /* Else if virtual format is too long for printf,
  855.      print in hex a byte at a time.  */
  856.       else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
  857.     {
  858.       register int j;
  859.       printf_filtered ("0x");
  860.       for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
  861.         printf_filtered ("%02x", (unsigned char)virtual_buffer[j]);
  862.     }
  863.       /* Else print as integer in hex and in decimal.  */
  864.       else
  865.     {
  866.       val_print (REGISTER_VIRTUAL_TYPE (i), raw_buffer, 0,
  867.              stdout, 'x', 1, 0, Val_pretty_default);
  868.       printf_filtered ("\t");
  869.       val_print (REGISTER_VIRTUAL_TYPE (i), raw_buffer, 0,
  870.              stdout,   0, 1, 0, Val_pretty_default);
  871.     }
  872.  
  873.       /* The SPARC wants to print even-numbered float regs as doubles
  874.      in addition to printing them as floats.  */
  875. #ifdef PRINT_REGISTER_HOOK
  876.       PRINT_REGISTER_HOOK (i);
  877. #endif
  878.  
  879.       printf_filtered ("\n");
  880.     }
  881. }
  882. #endif /* no DO_REGISTERS_INFO.  */
  883.  
  884. static void
  885. registers_info (addr_exp, fpregs)
  886.      char *addr_exp;
  887.      int fpregs;
  888. {
  889.   int regnum;
  890.  
  891.   if (!target_has_registers)
  892.     error ("The program has no registers now.");
  893.  
  894.   if (addr_exp)
  895.     {
  896.       if (*addr_exp >= '0' && *addr_exp <= '9')
  897.     regnum = atoi (addr_exp);
  898.       else
  899.     {
  900.       register char *p = addr_exp;
  901.       if (p[0] == '$')
  902.         p++;
  903.       for (regnum = 0; regnum < NUM_REGS; regnum++)
  904.         if (!strcmp (p, reg_names[regnum]))
  905.           break;
  906.       if (regnum == NUM_REGS)
  907.         error ("%s: invalid register name.", addr_exp);
  908.     }
  909.     }
  910.   else
  911.     regnum = -1;
  912.  
  913.   DO_REGISTERS_INFO(regnum, fpregs);
  914. }
  915.  
  916. static void
  917. all_registers_info (addr_exp)
  918.      char *addr_exp;
  919. {
  920.   registers_info (addr_exp, 1);
  921. }
  922.  
  923. static void
  924. nofp_registers_info (addr_exp)
  925.      char *addr_exp;
  926. {
  927.   registers_info (addr_exp, 0);
  928. }
  929.  
  930. /*
  931.  * TODO:
  932.  * Should save/restore the tty state since it might be that the
  933.  * program to be debugged was started on this tty and it wants
  934.  * the tty in some state other than what we want.  If it's running
  935.  * on another terminal or without a terminal, then saving and
  936.  * restoring the tty state is a harmless no-op.
  937.  * This only needs to be done if we are attaching to a process.
  938.  */
  939.  
  940. /*
  941.  * attach_command --
  942.  * takes a program started up outside of gdb and ``attaches'' to it.
  943.  * This stops it cold in its tracks and allows us to start tracing it.
  944.  * For this to work, we must be able to send the process a
  945.  * signal and we must have the same effective uid as the program.
  946.  */
  947. void
  948. attach_command (args, from_tty)
  949.      char *args;
  950.      int from_tty;
  951. {
  952.   dont_repeat ();            /* Not for the faint of heart */
  953.   target_attach (args, from_tty);
  954. }
  955.  
  956. /*
  957.  * detach_command --
  958.  * takes a program previously attached to and detaches it.
  959.  * The program resumes execution and will no longer stop
  960.  * on signals, etc.  We better not have left any breakpoints
  961.  * in the program or it'll die when it hits one.  For this
  962.  * to work, it may be necessary for the process to have been
  963.  * previously attached.  It *might* work if the program was
  964.  * started via the normal ptrace (PTRACE_TRACEME).
  965.  */
  966.  
  967. static void
  968. detach_command (args, from_tty)
  969.      char *args;
  970.      int from_tty;
  971. {
  972.   dont_repeat ();            /* Not for the faint of heart */
  973.   target_detach (args, from_tty);
  974. }
  975.  
  976. /* ARGSUSED */
  977. static void
  978. float_info (addr_exp)
  979.      char *addr_exp;
  980. {
  981. #ifdef FLOAT_INFO
  982.     FLOAT_INFO;
  983. #else
  984.     printf ("No floating point info available for this processor.\n");
  985. #endif
  986. }
  987.  
  988. struct cmd_list_element *unsetlist = NULL;
  989.  
  990. /* ARGSUSED */
  991. static void
  992. unset_command (args, from_tty)
  993.      char *args;
  994.      int from_tty;
  995. {
  996.   printf ("\"unset\" must be followed by the name of an unset subcommand.\n");
  997.   help_list (unsetlist, "unset ", -1, stdout);
  998. }
  999.  
  1000. void
  1001. _initialize_infcmd ()
  1002. {
  1003.   struct cmd_list_element *c;
  1004.   
  1005.   add_com ("tty", class_run, tty_command,
  1006.        "Set terminal for future runs of program being debugged.");
  1007.  
  1008.   add_show_from_set
  1009.     (add_set_cmd ("args", class_run, var_string_noescape, (char *)&inferior_args,
  1010.           
  1011. "Set arguments to give program being debugged when it is started.\n\
  1012. Follow this command with any number of args, to be passed to the program.",
  1013.           &setlist),
  1014.      &showlist);
  1015.  
  1016.   c = add_cmd
  1017.     ("environment", no_class, environment_info,
  1018.      "The environment to give the program, or one variable's value.\n\
  1019. With an argument VAR, prints the value of environment variable VAR to\n\
  1020. give the program being debugged.  With no arguments, prints the entire\n\
  1021. environment to be given to the program.", &showlist);
  1022.   c->completer = noop_completer;
  1023.  
  1024.   add_prefix_cmd ("unset", no_class, unset_command,
  1025.           "Complement to certain \"set\" commands",
  1026.           &unsetlist, "unset ", 0, &cmdlist);
  1027.   
  1028.   c = add_cmd ("environment", class_run, unset_environment_command,
  1029.           "Cancel environment variable VAR for the program.\n\
  1030. This does not affect the program until the next \"run\" command.",
  1031.        &unsetlist);
  1032.   c->completer = noop_completer;
  1033.  
  1034.   c = add_cmd ("environment", class_run, set_environment_command,
  1035.            "Set environment variable value to give the program.\n\
  1036. Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
  1037. VALUES of environment variables are uninterpreted strings.\n\
  1038. This does not affect the program until the next \"run\" command.",
  1039.        &setlist);
  1040.   c->completer = noop_completer;
  1041.  
  1042.   add_com ("path", class_files, path_command,
  1043.        "Add directory DIR(s) to beginning of search path for object files.\n\
  1044. $cwd in the path means the current working directory.\n\
  1045. This path is equivalent to the $PATH shell variable.  It is a list of\n\
  1046. directories, separated by colons.  These directories are searched to find\n\
  1047. fully linked executable files and separately compiled object files as needed.");
  1048.  
  1049.   c = add_cmd ("paths", no_class, path_info,
  1050.         "Current search path for finding object files.\n\
  1051. $cwd in the path means the current working directory.\n\
  1052. This path is equivalent to the $PATH shell variable.  It is a list of\n\
  1053. directories, separated by colons.  These directories are searched to find\n\
  1054. fully linked executable files and separately compiled object files as needed.", &showlist);
  1055.   c->completer = noop_completer;
  1056.  
  1057.  add_com ("attach", class_run, attach_command,
  1058.         "Attach to a process or file outside of GDB.\n\
  1059. This command attaches to another target, of the same type as your last\n\
  1060. `target' command (`info files' will show your target stack).\n\
  1061. The command may take as argument a process id or a device file.\n\
  1062. For a process id, you must have permission to send the process a signal,\n\
  1063. and it must have the same effective uid as the debugger.\n\
  1064. When using \"attach\", you should use the \"file\" command to specify\n\
  1065. the program running in the process, and to load its symbol table.");
  1066.  
  1067.   add_com ("detach", class_run, detach_command,
  1068.        "Detach a process or file previously attached.\n\
  1069. If a process, it is no longer traced, and it continues its execution.  If you\n\
  1070. were debugging a file, the file is closed and gdb no longer accesses it.");
  1071.  
  1072.   add_com ("signal", class_run, signal_command,
  1073.        "Continue program giving it signal number SIGNUMBER.");
  1074.  
  1075.   add_com ("stepi", class_run, stepi_command,
  1076.        "Step one instruction exactly.\n\
  1077. Argument N means do this N times (or till program stops for another reason).");
  1078.   add_com_alias ("si", "stepi", class_alias, 0);
  1079.  
  1080.   add_com ("nexti", class_run, nexti_command,
  1081.        "Step one instruction, but proceed through subroutine calls.\n\
  1082. Argument N means do this N times (or till program stops for another reason).");
  1083.   add_com_alias ("ni", "nexti", class_alias, 0);
  1084.  
  1085.   add_com ("finish", class_run, finish_command,
  1086.        "Execute until selected stack frame returns.\n\
  1087. Upon return, the value returned is printed and put in the value history.");
  1088.  
  1089.   add_com ("next", class_run, next_command,
  1090.        "Step program, proceeding through subroutine calls.\n\
  1091. Like the \"step\" command as long as subroutine calls do not happen;\n\
  1092. when they do, the call is treated as one instruction.\n\
  1093. Argument N means do this N times (or till program stops for another reason).");
  1094.   add_com_alias ("n", "next", class_run, 1);
  1095.  
  1096.   add_com ("step", class_run, step_command,
  1097.        "Step program until it reaches a different source line.\n\
  1098. Argument N means do this N times (or till program stops for another reason).");
  1099.   add_com_alias ("s", "step", class_run, 1);
  1100.  
  1101.   add_com ("until", class_run, until_command,
  1102.        "Execute until the program reaches a source line greater than the current\n\
  1103. or a specified line or address or function (same args as break command).\n\
  1104. Execution will also stop upon exit from the current stack frame.");
  1105.   add_com_alias ("u", "until", class_run, 1);
  1106.   
  1107.   add_com ("jump", class_run, jump_command,
  1108.        "Continue program being debugged at specified line or address.\n\
  1109. Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
  1110. for an address to start at.");
  1111.  
  1112.   add_com ("continue", class_run, continue_command,
  1113.        "Continue program being debugged, after signal or breakpoint.\n\
  1114. If proceeding from breakpoint, a number N may be used as an argument:\n\
  1115. then the same breakpoint won't break until the Nth time it is reached.");
  1116.   add_com_alias ("c", "cont", class_run, 1);
  1117.   add_com_alias ("fg", "cont", class_run, 1);
  1118.  
  1119.   add_com ("run", class_run, run_command,
  1120.        "Start debugged program.  You may specify arguments to give it.\n\
  1121. Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
  1122. Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
  1123. With no arguments, uses arguments last specified (with \"run\" or \"set args\".\n\
  1124. To cancel previous arguments and run with no arguments,\n\
  1125. use \"set args\" without arguments.");
  1126.   add_com_alias ("r", "run", class_run, 1);
  1127.  
  1128.   add_info ("registers", nofp_registers_info,
  1129.     "List of integer registers and their contents, for selected stack frame.\n\
  1130. Register name as argument means describe only that register.");
  1131.  
  1132.   add_info ("all-registers", all_registers_info,
  1133. "List of all registers and their contents, for selected stack frame.\n\
  1134. Register name as argument means describe only that register.");
  1135.  
  1136.   add_info ("program", program_info,
  1137.         "Execution status of the program.");
  1138.  
  1139.   add_info ("float", float_info,
  1140.         "Print the status of the floating point unit\n");
  1141.  
  1142.   inferior_args = savestring ("", 1);    /* Initially no args */
  1143.   inferior_environ = make_environ ();
  1144.   init_environ (inferior_environ);
  1145. }
  1146.